//
//  ParentViewController.m
//  老司机发车了
//
//  Created by 斌 on 16/8/9.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "ParentViewController.h"
#import "AppDelegate.h"

@interface ParentViewController ()
// 导航栏左边按钮的底view
@property (nonatomic,strong) UIView *myview;
// 左边按钮
@property (nonatomic,strong) UIButton *button;

@end

@implementation ParentViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    
    
    self.myview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setImage:[UIImage imageNamed:@"头像"] forState:UIControlStateNormal];
    _button.frame = CGRectMake(0, 0, 50, 50);
    [_button addTarget:delegate action:@selector(openDrawer) forControlEvents:UIControlEventTouchUpInside];
    [self.myview addSubview:_button];
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:_myview];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 添加观察者
    [delegate.drawerVC.leftView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    
    [[macro shareInstance] addObserver:self forKeyPath:@"isLogin" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    
    
}

// 当被观察者的属性的值发生改变之后 系统会自动在观察者的类中自动调用这个方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:@"frame"]) {
        
    UIView *view = object;
    // 改变导航栏按钮的不透明度
    if (view.frame.origin.x * (1.0 / (-123))<= 0.11) {
        self.myview.alpha = 0.11;
        return;
    }
    self.myview.alpha = view.frame.origin.x * (1.0 / (-123));
    } else {
        macro *m = object;
        if (m.isLogin == YES) {
            NSString *str = [macro shareInstance].userInfo.imgUrl;
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
            UIImage *img = [UIImage imageWithData:data];
            [self.button setImage:img forState:UIControlStateNormal];

        } else {
            [self.button setImage:[UIImage imageNamed:@"头像"] forState:UIControlStateNormal];
        }
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
